﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundUnderviser.ModelLayer;

namespace SundUnderviser.DBLayer
{
    public class DBAnamnese
    {
        private SqlCommand dbCmd = null;

        public List<Anamnese> FindAnamnese(int pnr)
        {
            string sql = @"select patientnr, nr, dato, vagt, infokilde, typeAfInfo, beskrivelse, antaldage from anamnese " +
                           " where patientnr  = " + pnr;


            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();
            List<Anamnese> anamList = new List<Anamnese>();
            Anamnese anamObj;

            while (dbReader.Read())
            {

                anamObj = new Anamnese();
                Console.WriteLine("antal felter " + dbReader.FieldCount);
                Patient patientObj = new Patient();
                patientObj.Nr = Convert.ToInt32(dbReader["patientnr"].ToString());
                anamObj.Patient = patientObj;    
                    anamObj.Nr = Convert.ToInt32(dbReader["nr"].ToString());
                    anamObj.Dato = Convert.ToDateTime(dbReader["dato"].ToString());
                    anamObj.Vagt = dbReader["vagt"].ToString();
                    anamObj.Type = dbReader["typeAfInfo"].ToString();
                    anamObj.Beskrivelse = dbReader["beskrivelse"].ToString();
                    anamObj.AntalDage = Convert.ToInt32(dbReader["antaldage"].ToString());
                    anamList.Add(anamObj);


                }
            dbReader.Close();
            DBConnection.Close();
            return anamList;
        }



        public int InsertAnamnese(Anamnese anamObj)
        {
            int rc = -1;
            SqlCommand dbCmd = null;
            try
            { // tildel naeste nr
                anamObj.Nr = 1;
               // 
                string sql = "select * from anamnese where patientnr = " + anamObj.Patient.Nr;
                SqlCommand dbCmd1 = DBConnection.GetDbCommand(sql);
                IDataReader dbReader;
                dbReader = dbCmd1.ExecuteReader();
                if (dbReader.Read())
                {
                    Console.WriteLine("har fundet nogle");
                    String sql2 = "select max(nr) as nextnr from anamnese where patientnr = " + anamObj.Patient.Nr;
                    DBConnection.Close();
                    SqlCommand dbCmd2 = DBConnection.GetDbCommand(sql2);
                    dbReader = dbCmd2.ExecuteReader();
                    Console.WriteLine("næstenr anaObj 1: " + anamObj.Nr);
                    if (dbReader.Read())
                    {
                        Console.WriteLine("tildel ny nr");
                        if (dbReader["nextnr"] != null)
                        {
                            anamObj.Nr = Convert.ToInt32(dbReader["nextnr"].ToString()) + 1;
                            Console.WriteLine("næstenr anaObj 2: " + anamObj.Nr);
                        }
                    }
                   
                }

                DBConnection.Close();
                string myDato = "" + anamObj.Dato.Year;
                if (anamObj.Dato.Month >= 10)
                {
                    myDato = myDato + anamObj.Dato.Month;
                }
                else
                {
                    myDato = myDato + "0" + anamObj.Dato.Month;
                }

                if (anamObj.Dato.Day >= 10)
                {
                    myDato = myDato + anamObj.Dato.Day;
                }
                else
                {
                    myDato = myDato + "0" + anamObj.Dato.Day;
                }
                string sqlInsert = "insert into anamnese (patientnr,nr, dato,vagt, infokilde, typeAfInfo, beskrivelse, antaldage)" +
                       "values (" +anamObj.Patient.Nr  + "," +
                       anamObj.Nr + ",' " + myDato+ "','" +
                       anamObj.Vagt + "','" +
                       anamObj.InfoKildeKontakt + "','" +
                       anamObj.Type + "', '" +
                       anamObj.Beskrivelse + "'," +
                       anamObj.AntalDage + ")";
                Console.WriteLine("anamnese instert " + sqlInsert);
                dbCmd = DBConnection.GetDbCommand(sqlInsert);

               
                rc = dbCmd.ExecuteNonQuery();


            }
            catch (Exception insertE)
            {
                Console.WriteLine("Fejl i insert anamnes");
                Console.WriteLine(insertE);
            }
            DBConnection.Close();
            return rc;
        }
    }
}
